﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace PRET.ReportControlLib.Controls
{
    public partial class SetMeterTimePanel : UserControl
    {
        public event ShowMeterReportViewEventHandle showReportEvent;
        public DateTime getStartTime
        {
            get
            {
                return this.startTimePicker.Value.Date;
            }
        }

        public DateTime getEndTime
        {
            get
            {
                return this.endTimePicker.Value.Date.AddDays(1);
            }
        }

        public float PeakUP
        {
            get
            {
                float peak = 0;
                float.TryParse(this.peakUPTextBox.Text, out peak);
                return peak;
            }
            set
            {
                this.peakUPTextBox.Text = value.ToString();
            }
        }

        public float GenUP
        {
            get
            {
                float gen = 0;
                float.TryParse(this.genUPTextBox.Text, out gen);
                return gen;
            }
            set
            {
                this.genUPTextBox.Text = value.ToString();
            }
        }

        public float LowUP
        {
            get
            {
                float low = 0;
                float.TryParse(this.lowUPTextBox.Text, out low);
                return low;
            }
            set
            {
                this.lowUPTextBox.Text = value.ToString();
            }
        }

        public void EnableControls(bool sign)
        {
            this.startTimePicker.Enabled = sign;
            this.endTimePicker.Enabled = sign;
            this.peakUPTextBox.Enabled = sign;
            this.genUPTextBox.Enabled = sign;
            this.lowUPTextBox.Enabled = sign;
            this.reportViewBtn.Enabled = sign;
        }

        public SetMeterTimePanel()
        {
            InitializeComponent();
            string startYear = string.Format("{0}/{1}/01", DateTime.Now.Year, DateTime.Now.Month);
            this.startTimePicker.Value = DateTime.Parse(startYear);
            this.endTimePicker.Value = DateTime.Today;
        }

        private void SetMeterTimePanel_Load(object sender, EventArgs e)
        {
            this.endTimePicker.MinDate = this.startTimePicker.Value;
            this.endTimePicker.MaxDate = DateTime.Now;
            this.startTimePicker.MaxDate = this.endTimePicker.Value;
        }

        private void startTimePicker_ValueChanged(object sender, EventArgs e)
        {
            this.endTimePicker.MinDate = this.startTimePicker.Value;
        }

        private void endTimePicker_ValueChanged(object sender, EventArgs e)
        {
            this.startTimePicker.MaxDate = this.endTimePicker.Value;
        }

        private void reportViewBtn_Click(object sender, EventArgs e)
        {
            if (showReportEvent != null)
            {
                ShowMeterReportViewEventArgs eventArg = new ShowMeterReportViewEventArgs();
                eventArg.StartTime = this.getStartTime;
                eventArg.EndTime = this.getEndTime;
                eventArg.PeakUP = this.PeakUP;
                eventArg.GenUP = this.GenUP;
                eventArg.LowUP = this.LowUP;
                showReportEvent(this, eventArg);
            }
        }
    }

    public delegate void ShowMeterReportViewEventHandle(object sender, ShowMeterReportViewEventArgs args);

    public class ShowMeterReportViewEventArgs : EventArgs
    {
        public ShowMeterReportViewEventArgs()
        { }
        private DateTime startTime;
        private DateTime endTime;
        private float peakUP;
        private float genUP;
        private float lowUP;

        public DateTime StartTime
        {
            get { return startTime; }
            set { startTime = value; }
        }
        public DateTime EndTime
        {
            get { return endTime; }
            set { endTime = value; }
        }
        public float PeakUP
        {
            get { return peakUP; }
            set { peakUP = value; }
        }
        public float GenUP
        {
            get { return genUP; }
            set { genUP = value; }
        }
        public float LowUP
        {
            get { return lowUP; }
            set { lowUP = value; }
        }
    }
}
